package cn.com.jslh.boarpro.util.template;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.R;

/**
 * Created by Administrator on 2017/11/17.
 */

public class ReMarkTempalte extends AddTempBaseItem implements AdapterInterface {
    @Override
    public String getFlag() {
        return "remark";
    }

    @Override
    public int getLayout() {
        return R.layout.add_template_remark_item_layout;
    }

    @Override
    public SlimInjector<? extends AddTempBaseItem> getSlimInjetor() {
        SlimInjector<ReMarkTempalte> inj = new SlimInjector<ReMarkTempalte>() {
            @Override
            public void onInject(ReMarkTempalte data, IViewInjector injector) {
                 JSONObject config = new JSONObject();
                try {
                    config = new JSONObject(data.getConfig());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                final JSONObject finalConfig = config;
                injector.text(R.id.titleText, data.getTitle())
                        .text(R.id.valueText, data.getPlaceholder())
                        .with(R.id.valueText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                              TextView textView =   (TextView)view;
                                try {
                                    String colorStr =finalConfig.getString("valueColor");
                                    if(colorStr.contains("#")&&colorStr.length()==7){
                                        textView.setTextColor(Color.parseColor(colorStr));
                                    }
                                    int valueSize =finalConfig.getInt("valueSize");
                                    if(valueSize!=-1){
                                        textView.setTextSize(valueSize);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        .with(R.id.titleText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                TextView textView =   (TextView)view;
                                try {
                                    String colorStr =finalConfig.getString("titleColor");
                                    if(colorStr.contains("#")&&colorStr.length()==7){
                                        textView.setTextColor(Color.parseColor(colorStr));
                                    }
                                    int valueSize =finalConfig.getInt("titleSize");
                                    if(valueSize!=-1){
                                        textView.setTextSize(valueSize);
                                    }
                                    int titleWidth =finalConfig.getInt("titleWidth");
                                    if(titleWidth!=-1){
                                        textView.setWidth(titleWidth);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
            }
        };
        return inj;
    }
}
